﻿using System.Linq;
using RpcClient;
using RpcHelper;
using RpcModel;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
        internal class RpcTraceService : IRpcTraceService
        {
                private readonly IRpcTraceCollect _Trace;
                private readonly IServerTypeCollect _ServerType;
                private readonly IServerRegionCollect _ServerRegion;
                public RpcTraceService (IRpcTraceCollect trace,
                        IServerTypeCollect serverType,
                        IServerRegionCollect region)
                {
                        this._ServerRegion = region;
                        this._ServerType = serverType;
                        this._Trace = trace;
                }
                public RpcTraceData[] Query (TraceQueryParam query, IBasicPage paging, out long count)
                {
                        RpcTrace[] list = this._Trace.Query (query, paging, out count);
                        if (list.Length == 0)
                        {
                                return null;
                        }
                        BasicServerType[] types = this._ServerType.GetBasic (list.Distinct (a => a.SystemTypeId));
                        ServerRegion[] regions = this._ServerRegion.Gets (list.Distinct (a => a.RegionId));
                     
                        return list.ConvertMap<RpcTrace, RpcTraceData> ((a, b) =>
                        {
                                b.SystemType = types.Find (c => c.Id == a.SystemTypeId, c => c.SystemName);
                                b.Region = regions.Find (c => c.Id == a.RegionId, c => c.RegionName);
                                return b;
                        });
                }
        }
}
